package chain

import (
	"fmt"
	_ "fmt"
	"sync"
	"time"
	_ "time"
)

const (
	group     = 5
	totalTask = 10
)

var wg = sync.WaitGroup{}

func Test() {
	fmt.Println("Test_chain")
}

func process(tasks chan int, workNum int) {
	defer wg.Done()
	for t := range tasks {
		fmt.Printf("Worker num %d is processing task%d \n", workNum, t)
	}

	fmt.Printf("Worker num %d is get off task \n", workNum)
}

func writeChain(c chan int, x int) {
	fmt.Println("writeChain")
	//fmt.Println(c)
	fmt.Println("Start", x)
	c <- x
	close(c)
	fmt.Println("End ", x)

	//close(c)
}

func main() {
	//c := make(chan int)
	//go writeChain(c, 1)
	//time.Sleep(1 * time.Second)
	//fmt.Println("Read", <-c)
	//if _, ok := <-c; ok {
	//	fmt.Println("Channel id open")
	//} else {
	//	fmt.Println("Channel id close")
	//}
	//channel := make(chan int, totalTask)
	//
	//for i := 0; i < group; i++ {
	//	wg.Add(1)
	//	go process(channel, i)
	//}
	//
	//for no := 1; no <= totalTask; no++ {
	//	channel <- no
	//}
	//
	//close(channel)
	//wg.Wait()

	ch1 := make(chan int)
	go func() {
		time.Sleep(4 * time.Second)
		ch1 <- 2
	}()

	select {
	case mess := <-ch1:
		fmt.Println("打印通道ch1值：" + string(rune(mess)))

	case t := <-time.After(2 * time.Second):
		fmt.Println("时间超时")
		fmt.Println(t)
	}

	ch2 := make(chan string)

	go func() {
		time.Sleep(4 * time.Second)
		ch2 <- "ch2 is ready"
	}()

	select {
	case t := <-ch2:
		fmt.Println(t)
	case t2 := <-time.After(4 * time.Second):
		fmt.Println(t2)

	}

}
